home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / C++ AppleLink Messages / CPlus.Dev$ 12⁄31⁄89 / 0031-Re ?Initializing a s-Dec89 < prev    next >
Encoding:
Text File  |  1990-01-02  |  2.3 KB  |  73 lines  |  [TEXT/GEOL]

  1. Item forwarded  by  ALCABES      to CPLUS.APPLE$
  2.  
  3. Item    9424175                         25-Dec-89        14:36
  4.  
  5. From:   D1622                           Nerdworks, Dan Weston,PRT
  6.  
  7. To:     MADA.EUROPE                     MacApp Dev Assoc Europe, E Carrasco
  8.  
  9. cc:     CPLUS.DEV$                      C++ Interest List--Developers
  10.  
  11. Sub:    Re: ?Initializing a static…
  12.  
  13. I ran into the same sort of confusion with static member initialization myself
  14. just last week.  The problem is that static members are not really members at
  15. all, rather, they are treated, storage-wise, like regular variables.  Take a
  16. look at the example below:  It has one static member and one regular member.
  17.  
  18.  class TFoo {
  19.       private:
  20.          static long   fSize;  // variable shared by all instances of the class
  21.                               // acessible from any method of the class
  22.          long  otherSize;       // Normal member
  23.  
  24.          /* ... */
  25.       public:
  26.          void IFoo(long size);
  27.          /* ... */
  28.       };
  29.  
  30.    void TFoo::IFoo(long size) {
  31.          this->fSize = size;  // kaboom!......why???
  32.           this->otherSize = size;
  33.       }
  34.  
  35.  
  36. Now look at the intermediate code (edited somewhat) that is generated when you
  37. compile the code shown above:  My extra comments are preceeded by //****
  38.  
  39.  
  40. //**** note that fSize is not in TFoo struct declaration
  41. struct TFoo {  /* sizeof TFoo == 4 */
  42.     long otherSize;
  43. };
  44.  
  45. //**** fSize is declared as extern, just like a global variable
  46. extern long fSize__4TFoo;
  47.  
  48.  
  49. //**** definition of IFoo member function
  50. void IFoo__4TFooFl(register struct TFoo *this, long size){
  51.     fSize__4TFoo= size;     //**** accessed like a global
  52.     this-> otherSize= size; //**** accessed like a member
  53. }
  54.  
  55.  
  56. You can see that the static member is not really a member of the TFoo struct.
  57. Static members are actually just like global variables with limited lexical
  58. access.  You can also see that C++ does not automatically allocate space for
  59. the static member, as evidenced by the 'extern' declaration.  It is up to you
  60. to define space for the static member with a definition/initialization
  61. statement, such as
  62.  
  63. long TFoo::fSize = 2;
  64.  
  65. I define and intialize static members in the same place that I define and
  66. initialize global variables for a file.
  67.  
  68. Hope this is helpful.
  69.  
  70. Dan Weston
  71. Nerdworks, D1622
  72.  
  73.